import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [

  {
    path: '/Login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../view/Login.vue')
  },
  {
    path: '/',
    redirect: '/Login'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../view/Home')
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 表示跳转到哪个路由上去 , from 表示从哪个路由跳转来
  // next 表示一个函数  执行 next() 表示放行,执行 next('/xxx') 表示强制跳转到/xxx页面
  if (to.path === '/login') {
    return next()
  } else {
    // 获取token
    var item = window.sessionStorage.getItem('token')
    if (!item) return next('/login')
    next()
  }
})

export default router
